using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using Antix;
using Antix.Data;

namespace Antix.Data.Catalogue.Objects
{
    #region Object Class

    /// <summary>
    /// Database Reference (foreign key constraint)
    /// </summary>
    public class DatabaseReference : DatabaseObject
    {
        #region Constructor/Defaults

        /// <summary>
        /// Create a new reference with defaults
        /// </summary>
        internal DatabaseReference(Database database)
            : this(null, null, null, DeleteActionEnum.None, database)
        {
            base.IsNew = true;
        }

        /// <summary>
        /// Create an existing reference object
        /// </summary>
        internal DatabaseReference(
            string nameOriginal,
            DatabaseColumn column,
            DatabaseTable parentTable, DeleteActionEnum deleteAction,
            Database database)
            : base(nameOriginal)
        {
            _Column = column;
            _ParentTable = parentTable;
            _DeleteAction = deleteAction;
            _Database = database;

            // drop all existing refs
            this.IsDeleted = true;
        }

        #endregion

        #region Properties

        #region Key

        /// <summary>
        /// Key for this object
        /// </summary>
        public override string Key
        {
            get { return this.Name; }
        }

        #endregion

        #region Database

        private Database _Database;

        /// <summary>
        /// Database
        /// </summary>
        public Database Database
        {
            get { return _Database; }
        }

        #endregion

        #region Column

        private DatabaseColumn _Column;

        /// <summary>
        /// Column for parent id
        /// </summary>
        public DatabaseColumn Column
        {
            get { return _Column; }
            set
            {
                if (_Column != value)
                {
                    _Column = value;
                    base.IsEdited = true;
                }
            }
        }

        #endregion

        #region ParentTable

        private DatabaseTable _ParentTable;

        /// <summary>
        /// Reference table for this relationship
        /// </summary>
        public DatabaseTable ReferencedTable
        {
            get { return _ParentTable; }
            set
            {
                if (_ParentTable != value)
                {
                    _ParentTable = value;
                    base.IsEdited = true;
                }
            }
        }

        #endregion

        #region DeleteAction

        private DeleteActionEnum _DeleteAction;

        /// <summary>
        /// Delete data when parent data is deleted (Cascade Delete)
        /// </summary>
        public DeleteActionEnum DeleteAction
        {
            get { return _DeleteAction; }
            set
            {
                if (_DeleteAction != value)
                {
                    _DeleteAction = value;
                    base.IsEdited = true;
                }
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Script the reference

        /// <summary>
        /// Generate script for this reference
        /// </summary>
        /// <param name="sql">Sql</param>
        public override bool Script(StringBuilder sql, bool subObjects)
        {
            return this.Database.Script(this, sql, subObjects);
        }

        #endregion

        #endregion
    }

    #endregion
}